package com.ccnu.demojava.net.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class RBioServer {
    /**
     * @author raohj
     * @date 2019年3月18日
     * @comment 堵塞模式，运行处理函数
     * @param 
     * @return 
     * @param sc
     */
    public void handInput(Socket sc){
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            in = new BufferedReader(new InputStreamReader(sc.getInputStream()));
            out = new PrintWriter(sc.getOutputStream(), true);
            String cliTxt = null;
            while(true){
            	out.println("00000362<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><Head><test3>111</test3><test4>111</test4><RetCd>1001</RetCd><RetMsg>交易出错</RetMsg></Head><Body><test1>111</test1><test2>111</test2><array_L1><code>111</code><name>222</name></array_L1><array_L1><code>111</code><name>222</name></array_L1><array_L1><code>111</code><name>222</name></array_L1></Body></service>");
//                out.println("00001567<?xml version='1.0' encoding='UTF-8'?><service><sys-header><data name=\"SYS_HEAD\"><struct><data name=\"RET\"><array><struct><data name=\"RET_MSG\"><field type=\"string\" length=\"9\">JDBC</field></data><data name=\"RET_CODE\"><field type=\"string\" length=\"6\">999999</field></data></struct></array></data><data name=\"MESSAGE_TYPE\"><field type=\"string\" length=\"4\">1410</field></data><data name=\"TRAN_TIMESTAMP\"><field type=\"string\" length=\"6\">151936</field></data><data name=\"BRANCH_ID\"><field type=\"string\" length=\"9\">100004301</field></data><data name=\"MESSAGE_CODE\"><field type=\"string\" length=\"4\">0198</field></data><data name=\"RET_STATUS\"><field type=\"string\" length=\"1\">F</field></data><data name=\"SERVICE_CODE\"><field type=\"string\" length=\"11\">SVR_INQUIRY</field></data><data name=\"SEQ_NO\"><field type=\"string\" length=\"21\">FPS001180604007186345</field></data></struct></data></sys-header><app-header><data name=\"APP_HEAD\"><struct><data name=\"PAGE_END\"><field type=\"string\" length=\"12\"></field></data><data name=\"CURRENT_NUM\"><field type=\"string\" length=\"12\"></field></data><data name=\"TOTAL_FLAG\"><field type=\"string\" length=\"1\"></field></data><data name=\"PGUP_OR_PGDN\"><field type=\"string\" length=\"1\"></field></data><data name=\"TOTAL_PAGES\"><field type=\"string\" length=\"12\"></field></data><data name=\"TOTAL_ROWS\"><field type=\"string\" length=\"12\"></field></data><data name=\"PAGE_START\"><field type=\"string\" length=\"12\"></field></data><data name=\"TOTAL_NUM\"><field type=\"string\" length=\"12\"></field></data></struct></data></app-header><local-header/><body/></service>");
                cliTxt = in.readLine();
                if(cliTxt == null){
                    break;
                }
                System.out.println(cliTxt);
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
				sc.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} // close the socket channel
		}
    }
    
    public class RTimerTask implements Runnable{
        private Socket sc;

        public RTimerTask(Socket sc) {
            this.sc = sc;
        }

        /**
         * When an object implementing interface <code>Runnable</code> is used
         * to create a thread, starting the thread causes the object's
         * <code>run</code> method to be called in that separately executing
         * thread.
         * <p>
         * The general contract of the method <code>run</code> is that it may
         * take any action whatsoever.
         *
         * @see Thread#run()
         */
        public void run() {
            BufferedReader in = null;
            PrintWriter out = null;
            try {
                in = new BufferedReader(new InputStreamReader(this.sc.getInputStream()));
                out = new PrintWriter(this.sc.getOutputStream(), true);
                String cliTxt = null;
                while(true){
                    //out.println("00001572<?xml version='1.0' encoding='UTF-8'?><service><sys-header><data name=\"SYS_HEAD\"><struct><data name=\"RET\"><array><struct><data name=\"RET_MSG\"><field type=\"string\" length=\"9\">JDBC调用失败!</field></data><data name=\"RET_CODE\"><field type=\"string\" length=\"6\">999999</field></data></struct></array></data><data name=\"MESSAGE_TYPE\"><field type=\"string\" length=\"4\">1410</field></data><data name=\"TRAN_TIMESTAMP\"><field type=\"string\" length=\"6\">151936</field></data><data name=\"BRANCH_ID\"><field type=\"string\" length=\"9\">100004301</field></data><data name=\"MESSAGE_CODE\"><field type=\"string\" length=\"4\">0198</field></data><data name=\"RET_STATUS\"><field type=\"string\" length=\"1\">F</field></data><data name=\"SERVICE_CODE\"><field type=\"string\" length=\"11\">SVR_INQUIRY</field></data><data name=\"SEQ_NO\"><field type=\"string\" length=\"21\">FPS001180604007186345</field></data></struct></data></sys-header><app-header><data name=\"APP_HEAD\"><struct><data name=\"PAGE_END\"><field type=\"string\" length=\"12\"></field></data><data name=\"CURRENT_NUM\"><field type=\"string\" length=\"12\"></field></data><data name=\"TOTAL_FLAG\"><field type=\"string\" length=\"1\"></field></data><data name=\"PGUP_OR_PGDN\"><field type=\"string\" length=\"1\"></field></data><data name=\"TOTAL_PAGES\"><field type=\"string\" length=\"12\"></field></data><data name=\"TOTAL_ROWS\"><field type=\"string\" length=\"12\"></field></data><data name=\"PAGE_START\"><field type=\"string\" length=\"12\"></field></data><data name=\"TOTAL_NUM\"><field type=\"string\" length=\"12\"></field></data></struct></data></app-header><local-header/><body/></service>");
                    out.println("00000314<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><Head><test3>111</test3><test4>111</test4></Head><Body><test1>111</test1><test2>111</test2><array_L1><code>111</code><name>222</name></array_L1><array_L1><code>111</code><name>222</name></array_L1><array_L1><code>111</code><name>222</name></array_L1></Body></service>");
//                    out.println("00002704<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><sys-header><data name=\"SYS_HEAD\" ><struct><data name=\"SERVICE_CODE\" ><field scale=\"0\" length=\"24\" type=\"string\" >SVR_FPS</field></data><data name=\"MESSAGE_TYPE\" ><field scale=\"0\" length=\"4\" type=\"string\" >1600</field></data><data name=\"MESSAGE_CODE\" ><field scale=\"0\" length=\"4\" type=\"string\" >1608</field></data><data name=\"SOURCE_TYPE\" ><field scale=\"0\" length=\"2\" type=\"string\" >CB</field></data><data name=\"BRANCH_ID\" ><field scale=\"0\" length=\"6\" type=\"string\" >001</field></data><data name=\"CONSUMER_ID\" ><field scale=\"0\" length=\"3\" type=\"string\" >IBS</field></data><data name=\"USER_ID\" ><field scale=\"0\" length=\"30\" type=\"string\" >9104</field></data><data name=\"TRAN_DATE\" ><field scale=\"0\" length=\"8\" type=\"string\" >20180620</field></data><data name=\"TRAN_TIMESTAMP\" ><field scale=\"0\" length=\"9\" type=\"string\" >214946073</field></data><data name=\"SERVER_ID\" ><field scale=\"0\" length=\"9\" type=\"string\" >127.0.0.1</field></data><data name=\"WS_ID\" ><field scale=\"0\" length=\"2\" type=\"string\" >01</field></data><data name=\"SEQ_NO\" ><field scale=\"0\" length=\"22\" type=\"string\" >IBS20180903145848000</field></data><data name=\"MODULE_ID\" ><field scale=\"0\" length=\"2\" type=\"string\" >CF</field></data><data name=\"USER_LANG\" ><field scale=\"0\" length=\"20\" type=\"string\" >CHINESE</field></data><data name=\"TRAN_TYPE\" ><field scale=\"0\" length=\"4\" type=\"string\" ></field></data><data name=\"TRAN_CODE\" ><field scale=\"0\" length=\"6\" type=\"string\" ></field></data><data name=\"TRAN_MODE\" ><field scale=\"0\" length=\"4\" type=\"string\" >ONLINE</field></data></struct></data></sys-header><app-header><data name=\"APP_HEAD\" ><struct><data name=\"PGUP_OR_PGDN\" ><field scale=\"0\" length=\"1\" type=\"string\" >0</field></data><data name=\"TOTAL_NUM\" ><field scale=\"0\" length=\"2\" type=\"string\" >-1</field></data></struct></data></app-header><local-header><data name=\"LOCAL_HEADER\" ><struct></struct></data></local-header><body><data name=\"cust_type\" ><field scale=\"0\" length=\"1\" type=\"string\" >1</field></data><data name=\"prod_code\" ><field scale=\"0\" length=\"32\" type=\"string\" ></field></data><data name=\"prod_day\" ><field scale=\"0\" length=\"12\" type=\"string\" >0</field></data><data name=\"is_save\" ><field scale=\"0\" length=\"1\" type=\"string\" >0</field></data><data name=\"income\" ><field scale=\"0\" length=\"1\" type=\"string\" >0</field></data><data name=\"prod_status\" ><field scale=\"0\" length=\"25\" type=\"string\" ></field></data><data name=\"prod_type\" ><field scale=\"0\" length=\"1\" type=\"string\" ></field></data><data name=\"pageNum\" ><field scale=\"0\" length=\"5\" type=\"string\" >1</field></data><data name=\"pageLength\" ><field scale=\"0\" length=\"5\" type=\"string\" >10</field></data></body></service>");
                    System.out.println("发送应答报文");
//                    cliTxt = in.readLine();
//                    if(cliTxt == null){
//                        break;
//                    }
//                    System.out.println(cliTxt);
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
//                try {
//					this.sc.close();
//				} catch (IOException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				} // close the socket channel
            }
        }
    }

    // 测试单线程
    public void testServer1() throws IOException{
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(20007);
            System.out.println("listen on port 20007");
            Socket sc = null;
            while(true){
                sc = ss.accept();
                handInput(sc);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(ss!=null){
                System.out.println("Server is closed");
                ss.close();
                ss = null;
            }
        }
    }

    // 测试多线程服务
    public void testServer2() throws IOException{
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(20007);
            System.out.println("listen on port 20007");            
            Socket sc = null;
            while(true){
                sc = ss.accept();
                System.out.println("Gate Way Connect in");
                new Thread(new RTimerTask(sc)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(ss!=null){
                System.out.println("Server is closed");
                ss.close();
                ss = null;
            }
        }
    }

    // 测试线程池服务
    public void testServer3() throws IOException{
        ServerSocket ss = null;
        try {
            ExecutorService testPool = Executors.newFixedThreadPool(200);
            ss = new ServerSocket(20007);
            System.out.println("listen on port 30001");            
            Socket sc = null;
            while(true){
                sc = ss.accept();
                testPool.submit(new RTimerTask(sc));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(ss!=null){
                System.out.println("Server is closed");
                ss.close();
                ss = null;
            }
        }
    }
}
